package com.easylinkin.linkappapi.bim.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author pwqoc
 */
@Data
public class TaskNode {

    private Integer id;
    private Integer parentId;
    private String name;
    private String startDate;
    private String endDate;
    private String duration;
    private BigDecimal completionStatus;
    private String rid;
    private String componentId;

    private List<TaskNode> children = new ArrayList<>();
    public void addSubTaskNode(TaskNode sub) {
        children.add(sub);
    }
    public List<TaskNode> getChildren() {
        calculateCompletedAndInProgressAndNotStartedYet();
        return children;
    }

    /**
     * 计算完成情况
     */
    public void calculateCompletedAndInProgressAndNotStartedYet() {
        int size = children.size();
        BigDecimal bigDecimal100 = BigDecimal.valueOf(100);

        if (size == 0) {
            if (completionStatus.compareTo(bigDecimal100) >= 0) {
                this.completed = completionStatus;
            } else if (completionStatus.compareTo(BigDecimal.ZERO) > 0) {
                this.inProgress = completionStatus;
            }else {
                this.notStartedYet = completionStatus;
            }
            return;
        }
        int completedCount = 0;
        int inProgressCount = 0;
        int notStartedYetCount = 0;

        for (TaskNode child : children) {
            BigDecimal childCompletionStatus = child.getCompletionStatus();
            if (childCompletionStatus.compareTo(bigDecimal100) >= 0) {
                completedCount++;
            } else if (childCompletionStatus.compareTo(BigDecimal.ZERO) > 0) {
                inProgressCount++;
            }else {
                notStartedYetCount++;
            }
        }
        BigDecimal sizeBigDecimal = BigDecimal.valueOf(size);
        this.completed = BigDecimal.valueOf(completedCount).divide(sizeBigDecimal,4, RoundingMode.HALF_UP)
                .multiply(bigDecimal100).setScale(2, RoundingMode.HALF_UP);
        this.inProgress = BigDecimal.valueOf(inProgressCount).divide(sizeBigDecimal,4, RoundingMode.HALF_UP)
                .multiply(bigDecimal100).setScale(2, RoundingMode.HALF_UP);
        this.notStartedYet = BigDecimal.valueOf(notStartedYetCount).divide(sizeBigDecimal,4, RoundingMode.HALF_UP)
                .multiply(bigDecimal100).setScale(2, RoundingMode.HALF_UP);
    }

    /*
    已完成
     */
    private BigDecimal completed = BigDecimal.ZERO;
    /*
    进行中
     */
    private BigDecimal inProgress = BigDecimal.ZERO;
    /*
    未开始
     */
    private BigDecimal notStartedYet = BigDecimal.ZERO;





    public static List<TaskNode> tree(List<TaskNode> nodeList, int pid, Map<Integer, TaskNode> nodeMap) {
        return nodeList.stream().peek(e -> {
            //父id为0  则为一级不处理
            if (ObjectUtil.notEqual(e.getParentId(), 0)) {
                TaskNode parent = nodeMap.get(e.getParentId());
                if (ObjectUtil.isNotNull(parent)) {
                    parent.addSubTaskNode(e);
                }
            }
        }).filter(e -> ObjectUtil.equals(e.getParentId(), pid)).collect(Collectors.toList());
    }
}
